use std::fs::File;
use std::io::Write;
use std::string::FromUtf8Error;

// 使用?简化代码
fn str_upper_match(str: Vec<u8>) -> Result<String, FromUtf8Error> {
    let ret = String::from_utf8(str).map(|s| s.to_uppercase())?;
    println!("Conversion succeeded: {}", ret);
    Ok(ret)
}
// fn str_upper_match(str: Vec<u8>) -> Result<String, FromUtf8Error> {
//     let ret = match String::from_utf8(str) {
//         Ok(str) => str.to_uppercase(),
//         Err(err) => return Err(err)
//     };
//
//     println!("Conversion succeeded: {}", ret);
//     Ok(ret)
// }
fn main() -> Result<(), std::io::Error> {
    let invalid_str = str_upper_match(vec![12, 101]);
    println!("{:?}", invalid_str);
    println!("{:?}", b"He");

    // 包含一系列的Result/Option的返回方法调用
    let _ = File::create("foo.txt")?.write_all(b"Hello world")?;

    // Option转换为Result
    let s = vec!["apple", "mango", "banana"];
    let fourth = s.get(4).ok_or("I got only 3 fruits");
    Ok(())
}
